// components/PayPasswordAuth/PayPasswordAuth.js
import { checkUserPassword } from '../../api/user';

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    password: '',
    payPasswordTrueFalseBy: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 开启密码输入弹框
    openPayAuth(){
      this.setData({
        payPasswordTrueFalseBy: true,
      })
    },
    // 关闭密码输入弹框
    onClosePayPassword(){
      this.setData({
        payPasswordTrueFalseBy: false,
      })
    },
    // 数字按钮
    _inputNumber(e){
      let num = e.target.dataset.number||e.currentTarget.dataset.number;
      console.log(num,'num')
      let password = this.data.password.length < 6 ? this.data.password + num.toString() : this.data.password
      this.setData({
        password: password
      })
      if(password.length == 6){
        this.allPasswordIpt()
      }
    },
    // 取消按钮
    _cancelNumber(){
      let password = this.data.password.length > 0 ? this.data.password.substring(0,this.data.password.length - 1) : '' 
      this.setData({
        password: password
      })
    },
    // 确认按钮
    _confirmNumber(){
      console.log(this.data.password,'password')
      if(this.data.password.length != 6){
        wx.showToast({
          title: '请输入完整的支付密码！',
          icon: 'none',
        })
        return
      }
      this.allPasswordIpt()
    },
    // 输入完支付密码
    allPasswordIpt(){
      let _this = this
      // 验证支付密码
      checkUserPassword({password:_this.data.password})
      .then(res=>{
        if(res.success){
          _this.onClosePayPassword()
          _this.triggerEvent('sonSubmitOrder',_this.data.password)
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
            duration: 2000
          })
        }
      })
    },
    clearPassword(){
      this.setData({
        password: ''
      })
    },
  }
})
